<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="会员ID">-->
<!--        <el-input v-model="queryParams.memberId" placeholder="请输入会员ID" clearable size="small"/>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="日期">-->
<!--        <el-date-picker-->
<!--            v-model="queryParams.date"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="选择日期"-->
<!--            size="small">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="车次">-->
<!--        <el-input v-model="queryParams.trainCode" placeholder="请输入车次" clearable size="small"/>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="状态">-->
<!--        <el-select v-model="queryParams.status" placeholder="订单状态" clearable size="small">-->
<!--          <el-option label="初始" value="I" />-->
<!--          <el-option label="处理中" value="P" />-->
<!--          <el-option label="失败" value="F" />-->
<!--          <el-option label="无票" value="E" />-->
<!--          <el-option label="取消" value="C" />-->
<!--          <el-option label="支付超时" value="EXPIRE" />-->
<!--          <el-option label="待支付" value="PAY" />-->
<!--          <el-option label="支付失败" value="PAY_FAILURE" />-->
<!--          <el-option label="已支付" value="PAID" />-->
<!--          <el-option label="已退款" value="REFUND" />-->
<!--          <el-option label="退款失败" value="REFUND_FAILURE" />-->
<!--          <el-option label="订单初始化" value="ORDER_INIT" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="primary"-->
    <!--          plain-->
    <!--          icon="el-icon-plus"-->
    <!--          size="mini"-->
    <!--          @click="handleAdd"-->
    <!--          v-hasPermi="['business:confirm-order:add']"-->
    <!--        >新增</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="success"-->
    <!--          plain-->
    <!--          icon="el-icon-edit"-->
    <!--          size="mini"-->
    <!--          :disabled="single"-->
    <!--          @click="handleUpdate"-->
    <!--          v-hasPermi="['business:confirm-order:edit']"-->
    <!--        >修改</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="danger"-->
    <!--          plain-->
    <!--          icon="el-icon-delete"-->
    <!--          size="mini"-->
    <!--          :disabled="multiple"-->
    <!--          @click="handleDelete"-->
    <!--          v-hasPermi="['business:confirm-order:remove']"-->
    <!--        >删除</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="warning"-->
    <!--          plain-->
    <!--          icon="el-icon-download"-->
    <!--          size="mini"-->
    <!--          @click="handleExport"-->
    <!--          v-hasPermi="['business:confirm-order:export']"-->
    <!--        >导出</el-button>-->
    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table v-loading="loading" :data="confirmOrderList" :row-style="{ height: '60px' }" @selection-change="handleSelectionChange">
      <!--      <el-table-column show-overflow-tooltip type="selection" width="55" align="center" />-->
      <el-table-column show-overflow-tooltip label="id" align="center" prop="id" />
      <el-table-column show-overflow-tooltip label="会员id" align="center" prop="memberId" />
      <el-table-column show-overflow-tooltip label="日期" align="center" prop="date" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="车次编号" align="center" prop="trainCode" />
      <el-table-column show-overflow-tooltip label="出发站" align="center" prop="start" />
      <el-table-column show-overflow-tooltip label="到达站" align="center" prop="end" />
      <el-table-column show-overflow-tooltip label="余票ID" align="center" prop="dailyTrainTicketId" />
      <el-table-column show-overflow-tooltip label="车票" align="center" prop="tickets">
        <template slot-scope="scope">
          <span>{{ getTicketsCount(scope.row.tickets) }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{ formatStatus(scope.row) }}</el-tag>
        </template>
      </el-table-column>
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="handleUpdate(scope.row)"-->
      <!--            v-hasPermi="['business:confirm-order:edit']"-->
      <!--          >修改</el-button>-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-delete"-->
      <!--            @click="handleDelete(scope.row)"-->
      <!--            v-hasPermi="['business:confirm-order:remove']"-->
      <!--          >删除</el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改订单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="会员id" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入会员id" />
        </el-form-item>
        <el-form-item label="日期" prop="date">
          <el-date-picker clearable
                          v-model="form.date"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车次编号" prop="trainCode">
          <el-input v-model="form.trainCode" placeholder="请输入车次编号" />
        </el-form-item>
        <el-form-item label="出发站" prop="start">
          <el-input v-model="form.start" placeholder="请输入出发站" />
        </el-form-item>
        <el-form-item label="到达站" prop="end">
          <el-input v-model="form.end" placeholder="请输入到达站" />
        </el-form-item>
        <el-form-item label="余票ID" prop="dailyTrainTicketId">
          <el-input v-model="form.dailyTrainTicketId" placeholder="请输入余票ID" />
        </el-form-item>
        <el-form-item label="订单状态" prop="status">
          <el-input v-model="form.status" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfirmOrder, getConfirmOrder, delConfirmOrder, addConfirmOrder, updateConfirmOrder } from "@/api/business/confirm-order";

export default {
  name: "Confirm-order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单信息表格数据
      confirmOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: null,
        date: null,
        trainCode: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        memberId: [
          { required: true, message: "会员id不能为空", trigger: "blur" }
        ],
        date: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        trainCode: [
          { required: true, message: "车次编号不能为空", trigger: "blur" }
        ],
        start: [
          { required: true, message: "出发站不能为空", trigger: "blur" }
        ],
        end: [
          { required: true, message: "到达站不能为空", trigger: "blur" }
        ],
        dailyTrainTicketId: [
          { required: true, message: "余票ID不能为空", trigger: "blur" }
        ],
        tickets: [
          { required: true, message: "车票不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "订单状态不能为空", trigger: "blur" }
        ],
      },
      // 状态字典
      statusOptions: [
        { label: '初始', value: 'I' },
        { label: '处理中', value: 'P' },
        { label: '失败', value: 'F' },
        { label: '无票', value: 'E' },
        { label: '取消', value: 'C' },
        { label: '支付超时', value: 'EXPIRE' },
        { label: '待支付', value: 'PAY' },
        { label: '支付失败', value: 'PAY_FAILURE' },
        { label: '已支付', value: 'PAID' },
        { label: '已退款', value: 'REFUND' },
        { label: '退款失败', value: 'REFUND_FAILURE' },
        { label: '订单初始化', value: 'ORDER_INIT' }
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单信息列表 */
    getList() {
      this.loading = true;
      listConfirmOrder(this.queryParams).then(response => {
        this.confirmOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        memberId: null,
        date: null,
        trainCode: null,
        start: null,
        end: null,
        dailyTrainTicketId: null,
        tickets: null,
        status: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getConfirmOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConfirmOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfirmOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单信息编号为"' + ids + '"的数据项？').then(function () {
        return delConfirmOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/confirm-order/export', {
        ...this.queryParams
      }, `confirm-order_${new Date().getTime()}.xlsx`)
    },
    // 格式化状态显示
    formatStatus(row) {
      const status = this.statusOptions.find(item => item.value === row.status);
      return status ? status.label : row.status;
    },
    // 获取车票数量
    getTicketsCount(tickets) {
      try {
        const ticketsArray = JSON.parse(tickets);
        return ticketsArray.length + '张';
      } catch (e) {
        return '0张';
      }
    },
    // 获取状态对应的样式类型
    getStatusType(status) {
      switch (status) {
        case 'I':
        case 'ORDER_INIT':
          return 'info';    // 灰色
        case 'P':
          return '';        // 默认蓝色
        case 'F':
        case 'PAY_FAILURE':
        case 'REFUND_FAILURE':
          return 'danger';  // 红色
        case 'E':
          return 'warning'; // 黄色
        case 'C':
          return 'info';    // 灰色
        case 'EXPIRE':
          return 'danger';  // 红色
        case 'PAY':
          return 'warning'; // 黄色
        case 'PAID':
          return 'success'; // 绿色
        case 'REFUND':
          return 'info';    // 灰色
        default:
          return '';        // 默认蓝色
      }
    }
  }
};
</script>
